<?php
/**
 * Class that operate on table 'top_stories'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class TopStoriesMySqlDAO implements TopStoriesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return TopStoriesMySql 
	 */
	public function load($topStorie){
		$sql = 'SELECT * FROM top_stories WHERE topstories_id = "'.$topStorie->topstories_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll($topStorie){
		$sql = 'SELECT * FROM top_stories order by '.$topStorie->order_by.' '.$topStorie->sort_order.' limit '.$topStorie->results_per_page.' offset '.$topStorie->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	public function topStories_count($topStorie){
		$sql = 'SELECT count(*) FROM top_stories order by '.$topStorie->order_by.' '.$topStorie->sort_order.' limit '.$topStorie->results_per_page.' offset '.$topStorie->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllSearchBy($topStorie){
		$sql = 'SELECT * FROM top_stories WHERE '.$topStorie->search_by.' like "%'.$topStorie->search_by_value.'%" order by '.$topStorie->order_by.' '.$topStorie->sort_order.' limit '.$topStorie->results_per_page.' offset '.$topStorie->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function searchBy_count($topStorie){
		$sql = 'SELECT count(*) FROM top_stories WHERE '.$topStorie->search_by.' like "%'.$topStorie->search_by_value.'%" order by '.$topStorie->order_by.' '.$topStorie->sort_order.' limit '.$topStorie->results_per_page.' offset '.$topStorie->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	/**
 	 * Delete record from table
 	 * @param topStorie primary key
 	 */
	public function delete($topstories_id){
		$sql = 'DELETE FROM top_stories WHERE topstories_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($topstories_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	public function delete_topStories($topStorie){
		$sql = 'UPDATE top_stories SET status = "'.$topStorie->status.'" WHERE topstories_id = "'.$topStorie->topstories_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param TopStoriesMySql topStorie
 	 */
	public function insert($topStorie){
		$sql = 'INSERT INTO top_stories (topstories_title, topstories_date, topstories_content, topstories_author, topstories_image, created_date, updated_date) VALUES ("'.$topStorie->topstories_title.'", "'.$topStorie->topstories_date.'", "'.$topStorie->topstories_content.'", "'.$topStorie->topstories_author.'", "'.$topStorie->topstories_image.'", "'.$topStorie->created_date.'", "'.$topStorie->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		
		/* $sqlQuery->set($topStorie->topstoriesTitle);
		$sqlQuery->set($topStorie->topstoriesDate);
		$sqlQuery->set($topStorie->topstoriesContent);
		$sqlQuery->set($topStorie->topstoriesAuthor);
		$sqlQuery->set($topStorie->topstoriesImage);
		$sqlQuery->set($topStorie->created_date);
		$sqlQuery->set($topStorie->updated_date);
		$sqlQuery->setNumber($topStorie->status);
 */
		$id = $this->executeInsert($sqlQuery);	
		$topStorie->topstoriesId = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param TopStoriesMySql topStorie
 	 */
	public function update($topStorie){
		$sql = 'UPDATE top_stories SET topstories_title = ?, topstories_date = ?, topstories_content = ?, topstories_author = ?, topstories_image = ?, updated_date = ?, status = ? WHERE topstories_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($topStorie->topstories_title);
		$sqlQuery->set($topStorie->topstories_date);
		$sqlQuery->set($topStorie->topstories_content);
		$sqlQuery->set($topStorie->topstories_author);
		$sqlQuery->set($topStorie->topstories_image);
		$sqlQuery->set($topStorie->updated_date);
		$sqlQuery->setNumber($topStorie->status);

		$sqlQuery->setNumber($topStorie->topstories_id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM top_stories';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByTopstoriesTitle($value){
		$sql = 'SELECT * FROM top_stories WHERE topstories_title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTopstoriesDate($value){
		$sql = 'SELECT * FROM top_stories WHERE topstories_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTopstoriesContent($value){
		$sql = 'SELECT * FROM top_stories WHERE topstories_content = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTopstoriesAuthor($value){
		$sql = 'SELECT * FROM top_stories WHERE topstories_author = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTopstoriesImage($value){
		$sql = 'SELECT * FROM top_stories WHERE topstories_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM top_stories WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM top_stories WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM top_stories WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByTopstoriesTitle($value){
		$sql = 'DELETE FROM top_stories WHERE topstories_title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTopstoriesDate($value){
		$sql = 'DELETE FROM top_stories WHERE topstories_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTopstoriesContent($value){
		$sql = 'DELETE FROM top_stories WHERE topstories_content = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTopstoriesAuthor($value){
		$sql = 'DELETE FROM top_stories WHERE topstories_author = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTopstoriesImage($value){
		$sql = 'DELETE FROM top_stories WHERE topstories_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM top_stories WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM top_stories WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM top_stories WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return TopStoriesMySql 
	 */
	protected function readRow($row){
		$topStorie = new TopStorie();
		
		$topStorie->topstories_id = $row['topstories_id'];
		$topStorie->topstories_title = $row['topstories_title'];
		$topStorie->topstories_date = $row['topstories_date'];
		$topStorie->topstories_content = $row['topstories_content'];
		$topStorie->topstories_author = $row['topstories_author'];
		$topStorie->topstories_image = $row['topstories_image'];
		$topStorie->created_date = $row['created_date'];
		$topStorie->updated_date = $row['updated_date'];
		$topStorie->status = $row['status'];

		return $topStorie;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return TopStoriesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>